using System;
using Bugzproxy;

class CreateBugClass {
  static public int Main( string[] args ) {
    ////////////////////////////////////////////////////////////
    // Use common class to get all arguments
    SimpleAppArgs argParser 
      = new SimpleAppArgs( "CreateBug.exe", 
                           "Program to create a single (fixed) bug on a server" );
    // If parsing fails, bail out
    if ( ! argParser.Parse( args ) ) { 
      return 1; 
    }
    Console.WriteLine( "Using server {0}:{1}/{2}", argParser.Host, argParser.Port, argParser.Path );
    Console.WriteLine( "Using username {0} and password {1}", argParser.User, argParser.Password );
    // Construct server, based on arguments, setup tracer if user set option
    Server server 
      = new Server( argParser.Host, argParser.Port, argParser.Path );
    if ( argParser.Trace ) { 
      server.TraceWriter = System.Console.Out; 
    }
    
    ////////////////////////////////////////////////////////////
    // Real program starts here
 

    // A number of values are "defaulted", that is, in a vanilla
    // Bugzilla installation they are optional. However, they can be
    // required in some installations. If that is the case, you must
    // supply values for them, in order for this to work. Otherwise,
    // default/empty values will be used
    Bug b;
    try {      
      server.Login( argParser.User, argParser.Password, true );
      b = server.GetProduct( 1 ).CreateBug( null, // alias
                                            "WeatherControl", // component 
                                            "1.0", // version
                                            "All", // operating system
                                            "All", // platform 
                                            "Test bug from bugzproxy (samples/CreateBug)", // summary
                                            "This bug was autocreated by the bugzproxy CreateBug test program", // initial description
                                            "P2", // priority
                                            "normal", // severity
                                            null, // status
                                            null, // milestone
                                            null, // assigned to
                                            null, // list of cc's
                                            null  // quality contact
                                            );
    }
     catch (Exception e) {
      Console.WriteLine( "An exception occured. Try running with --trace? Exception follows");
      Console.WriteLine( e );
      return 1;
    }
    Console.WriteLine( "Succesfully created bug with id {0}", b.Id );
    return 0;
  }
}
